import wxValidate from 'wechat-validate'
Page({
  behaviors:[wxValidate],
  rules:{
    houseId: [
      {required: true, message: '请选择报修房屋!'}
    ],
    repairItemId: [
      {required: true, message: '请选择维修的项目!'}
    ],
    mobile: [
      {required: true, message: '请填写手机号码!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'},
    ],
    appointment: [
      {required: true, message: '请选择预约日期!'}
    ],
    description: [
      {required: true, message: '请填写问题描述!'}
    ],
},
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [],
    houseId:'',
    houseInfo:'',
    repairItemId:'',
    repairItemName:'',
    appointment: '',
    description:'',
    mobile:'',
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // goList() {
  //   wx.reLaunch({
  //     url: '/repair_pkg/pages/list/index',
  //   })
  // },
  onLoad({id}){
    this.getHouseList()
    this.getRepairItem()
    if(id){
      this.getRepairDetail(id)
      wx.setNavigationBarTitle({
        title:id? '修改报修信息':'填写报修信息',
      })
    }
  },
  async getRepairDetail(id){
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ ...repairDetail })
  },
  async getHouseList(){
    const {code,data}=await wx.http.get('/house')
    if(code!==10000) wx.utils.toast()
    this.setData({
      houseList:data
    })
  },
   // 获取维修项目
   async getRepairItem() {
    // 调用接口
    const { code, data: repairItem } = await wx.http.get('/repairItem')
    // 检测接口是否调用成功
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ repairItem })
  },
  selectHouseInfo(ev){
     // 数据渲染
     this.setData({
      houseId: ev.detail.id,
      houseInfo: ev.detail.name,
    })
  },
  // 获取用户选择的维修项目
  selectRepairItemInfo(ev) {
    this.setData({
      repairItemId: ev.detail.id,
      repairItemName: ev.detail.name,
    })
  },
  selectDateInfo(ev){
    console.log(ev);
    this.setData({
      appointment:wx.utils.dataFormat(ev.detail),
      dateLayerVisible: false
    })
  },
  uploadPicture(ev){
    const file=ev.detail.file
    wx.uploadFile({
      filePath: file.url,
      name: 'file',
      url: wx.http.baseURL + '/upload',
      header: {
        Authorization: 'Bearer ' + getApp().token,
      },
      success:(res)=>{
        const data = JSON.parse(res.data)
        // 先获取原来已经上传的图片
        const { attachment } = this.data
        // 追加新的上传的图片
        attachment.push(data.data)
        this.setData({attachment})
      }
    })
  },
  deletePic(ev){
    this.setData({attachment:this.data.attachment.filter(item=>item.id!==ev.detail.file.id)})
  },
  async submitForm(){
    if(!this.validate()) return
    const {id,houseId, repairItemId, mobile, appointment, description, attachment} = this.data
    const {code}=await wx.http.post('/repair',{
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
    })
    if (code !== 10000) return wx.utils.toast('在线报修失败!')
    wx.redirectTo({
      url:'/repair_pkg/pages/list/index'
    })
  }
})
